/*******************************************************************************
 ** Name: Socket.cpp                                                          **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved 2-clause BSD License                **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Created on March 16, 2010, 9:48 PM
// Modified $Date: 2011-09-18 21:48:59 +0000 (Sun, 18 Sep 2011) $ by $Author: gian.james $
#define  Socket_cpp_ID    "$Id: Socket.cpp 145 2011-09-18 21:48:59Z gian.james $"

extern "C" {
#include <string.h>
}
#include <wchar.h>
#include "Socket.hpp"
#include "common.hpp"

// Stream operations/operators
Socket &
Socket::operator << (Socket & s)
{
	// TODO ?
	return *this;
}

Socket &
Socket::operator << (const char * c)
{
	send(sockID,c,strlen(c),0);
	return *this;
}


Socket &
Socket::operator << (const std::string s)
{
	send(sockID,s.data(),s.length(),0);
	return *this;
}

//Socket &
//Socket::operator << (int i)
//{
//	char buff[12];
//	sprintf(buff,"%d",i);
//	send(sockID,buff,strlen(buff),0);
//	return *this;
//}


void
Socket::WriteLine(int s, const char * msg)
{
    send(s,msg,strlen(msg),0);
    send(s,"\r\n",2,0);
}



void
Socket::Write(int s, const char * msg)
{
    send(s,msg,strlen(msg),0);
}


// ReadLine is used repeatedly with the same buffer so we just zero
// the buffer here instead of having ZERO()'s strewn all over
int
Socket::ReadLine(int s, char * buff, int sz)
{
	ZERO(buff,sz);
    return recv(s,buff,sz,0);
}



int
Socket::Connect(const char* h, int p)
{
    int rc;
    struct sockaddr_in  addr;
    int len = BuildSockAddr(addr,h,p);
    if ((rc = connect(sockID,(sockaddr*)&addr,len)) < 0)
        bIsOK = false;
    return rc;
}

int
Socket::BuildSockAddr(sockaddr_in& si, const char* h, int p)
{
    si.sin_family = domain;
    if (h == NULL)
        si.sin_addr.s_addr = INADDR_ANY;
    else
    {
        hostent * hp = gethostbyname(h);
        if (hp == 0)
            return -1;
        memcpy((char*)&si.sin_addr,(char*)hp->h_addr,hp->h_length);
    }
    si.sin_port = htons(p);
    return sizeof(si);
}

int
Socket::Accept()
{
    int             cliSock;
    socklen_t       cliLen;
//    sockaddr_in     cliAddr;

    cliLen  = sizeof(cliAddr);
    cliSock = accept(sockID, (sockaddr*)&cliAddr, &cliLen);

    if (cliSock < 0) bIsOK = false;
    return cliSock;
}

int
Socket::Bind(const char* n, int p)
{
    int rc;
    struct sockaddr_in  addr;
    int len = BuildSockAddr(addr,n,p);
    if ((rc = bind(sockID,(sockaddr*)&addr,len)) < 0)
        bIsOK = false;

    if (rc != -1 && sockType != SOCK_DGRAM && (rc = listen(sockID,LISTEN_NUM)) < 0)
        bIsOK = false;
    return rc;
}

// Constructors, destructors
Socket::Socket(int d, int t, int p)
{
    domain = d; sockType = t; proto = p;
    if ((sockID = socket(domain,sockType,proto)) < 0)
        bIsOK = false;
    else
        bIsOK = true;
}

Socket::Socket(const Socket& orig)
{

}

Socket::~Socket()
{

}

